function idat = fsb_denoise_idat(idat,threshold,disp)

% FSB : remove noise from MRI images
%
% EXAMPLE:
% idat =  fsb_denoise_idat(idat_spare,intensity_threshold,0);
%
% INPUT:
% idat: image file in either 3D or 4D
% threshold: threshold above which image information is kept. Everything under the threshold is set to 0.
% disp: The disp switch allows for graphical display of results
%
% OUTPUT:
% idat: denoised 4D image data
% Copyright Steffen Stoewer 28/11/07
%
% CALLED BY:
% FSB.m
%
% Copyright 2010 MPI for Biological Cybernetics
% Author: Steffen Stoewer
% License:GNU GPL, no express or implied warranties
% 
% $ Revision 1.0
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

%~~~~~~~~~~~~~~~~~~~~~
% Actual thresholding
%~~~~~~~~~~~~~~~~~~~~~
class_idat = class(idat);
mask_idat = idat>=threshold; % masks image with threshold
mask_idat =eval([class_idat '(mask_idat)']); % changes logical mask_idat to idat datatype
idat = idat.*mask_idat; % applies threshold to image

%~~~~~~~~~~~~~~~~~~~~~
%Displays graphical output if desired
%~~~~~~~~~~~~~~~~~~~~~

if disp == 1;
    [a,b,c,d]=size(idat);

    midl = c/2; %finds middle slice

    figure;
    subplot(2,2,1);
    imagesc(idat(:,:,midl,1));
    idat = idat.*mask_idat; % applies threshold to image
    subplot(2,2,2);
    imagesc(idat(:,:,midl,1));
else
    idat = idat.*mask_idat; % applies threshold to image
end
end
